package com.taoz.rpc.core.loadbalancer;

import cn.hutool.core.collection.CollUtil;
import com.taoz.rpc.core.model.ServiceMetaInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
public class RandomLoadBalancer implements LoadBalancer {
    private final Random random = new Random();

    @Override
    public ServiceMetaInfo selectService(List<ServiceMetaInfo> serviceList, Map<String, Object> balanceParams) {
        log.info("正在使用 {} 获取服务", "RandomLoadBalancer");
        if (CollUtil.isEmpty(serviceList)) {
            return null;
        } else if (serviceList.size() == 1) {
            return serviceList.get(0);
        }
        return serviceList.get(random.nextInt(serviceList.size()));
    }
}
